<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <!-- <link rel="stylesheet" href="../static/css/my_log_register.css"> -->
    <link rel="stylesheet" href="../static/css/login.css">

    <style>
        /* 提示框样式 */
        .alert{
            background-color: azure;
            width: 27.5rem; 
            margin-top: -0.5rem;
            font-family: cursive;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="container" class="p-3 mb-5 bg-white rounded shadow" style="font-family: cursive;">
        <!-- 登录标题框 -->
        <div id="title" style="margin-bottom: 2rem;">
            <h1 style="text-align: center; padding-top: 0.5rem;">登 陆</h1>
        </div>

        <form id="loginForm" action="/loginDone" method="POST">
            <!-- 文本框 -->
            <div id="body_middle">
                <!-- 账号框 -->
                <div class="row">
                    <div class="col">
                        <center>
                            <input type="text" class="form-control" id="username" name="username"
                                placeholder="用户名/手机号/邮箱" style="width: 50%; text-align: center;">
                        </center>
                    </div>
                </div>
                <!-- 密码框 -->
                <div class="row">
                    <div class="col">
                        <center>
                            <input type="password" class="form-control" id="password" name="password"
                                placeholder="请输入密码" style="width: 50%; text-align: center">
                            <!-- 功能小眼睛 -->
                            <img src="../static/image/eye-slash.svg" alt="" id="eyes"
                                style="margin-right: 6.6rem; margin-top: 0px; ">
                        </center>
                    </div>
                </div>
            </div>

            <!-- 辅助板块 -->
            <div>
                <div class="row">
                    <div class="col">

                    </div>
                </div>
                <!-- 按钮组件 -->
                <div style="text-align: center;">
                    <!-- <center></center> -->
                    <div class="form-group-3 form-check">
                        <input type="checkbox" class="form-check-input" id="exampleCheck1">
                        <label class="form-check-label" for="exampleCheck1">
                            <h6>Remember Me</h6>
                        </label>
                    </div>
                    <button type="button" class="btn btn-outline-success" style="width: 14.5rem;" onclick="jumpTo()">登录</button>
                </div>
                <div class="form-group btn">
                    <a href="../view/register.html" type="button" onclick="jump()">
                        <h6>注册账号</h6>
                    </a>
                </div>
            </div>
        </form>

        <!-- 提示 -->
        <div class="col">
            <div class="alert alert-danger alert-dismissible fade show invisible" role="alert">
                <strong class="alert-dan">提示：</strong><label for="" id="msg">敦煌</label>
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
        </div>
    </div>

    <script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
    <script src="../static/js/jquery-3.5.1.min.js"></script>
    <script src="../static/js/bootstrap.min.js"></script>
    <!-- 登录页输入框焦点逻辑 -->
    <script src="../static/js/login_input_focus.js"></script>
    <!-- 小眼睛——显示隐藏密码实现逻辑 -->
    <script src="../static/js/icon_click.js"></script>

    <script>
        function jump() {
            window.location.href = "/register";
        }
        function jumpTo() {
            let username = $('#username').val();
            let password = $('#password').val();

            if (username.length > 0 && password.length > 0) {
                axios({
                    method: "post",
                    url: "/loginDone",
                    data: $("#loginForm").serialize()
                }).then(function (res) {
                    // axios工具下，服务器真正返回的数据在返回对象的data中
                    res = res.data;
                    if (res.Code === 200) {
                        window.location.href = "/";
                    } else {
                        alert_fn(res.msg)
                    }
                }).catch((err) => {
                    alert_fn(err),
                        console.log(err);
                })
            } else {
                alert_fn("用户名、密码不能为空")
            }
        }
        // 这个方法包装了alert的相关行为和显示的内容
        function alert_fn(msg) {
            msg = msg || '密码不能为空，请输入正确密码';
            // 替换提示的内容
            $('#msg').text(msg);
            // 移除不显示的class，则alert将显示
            $('.alert').removeClass('invisible');
            // 三秒中后，给alert增加不显示的class，则alert重新隐藏
            setTimeout(() => {
                $('.alert').addClass('invisible');
            }, 3000);
        }
    </script>
</body>
</html>